package com.leixiaoqiao.controller;

import com.github.pagehelper.PageInfo;
import com.leixiaoqiao.api.Result;
import com.leixiaoqiao.pojo.Book;
import com.leixiaoqiao.service.BookService;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.*;

import java.util.List;

/**
 * @description: 用户控制器
 * @company: 云桥之上
 * @author: 萌萌居家好男人
 * @version: 1.0
 * @create: 2021-11-21 20:17:27
 */
@RestController
@RequestMapping("/book")
public class BookHandler {
    @Autowired
    private BookService bookService;

    @GetMapping("/list")
    public List<Book> getBooks(){
        List<Book> bookList= bookService.getBooks();
        return bookList;
    }

    //前端请求地址：http://localhost:8080/SSM/book/page/1/3
    @GetMapping("/page/{pageNum}/{pageSize}")
    //restful风格
    public Result<PageInfo<Book>> getPageBook(@PathVariable Integer pageNum,@PathVariable Integer pageSize){
        Result<PageInfo<Book>> result = new Result<>();
        //调用业务方法
        PageInfo<Book> pageInfo = bookService.page(pageNum,pageSize);
        result.setCode(200);//不允许直接写固定值，实际开发中应该使用静态常量代替固定值方式
        result.setSuccess(true);
        result.setData(pageInfo);
        result.setMessage("分页查询成功");
        return result;
    }


    @GetMapping("/{id}")
    public Result<?> getBook(@PathVariable Integer id){
        Result<Object> result = new Result<>();

        Book book = bookService.getBook(id);

        if(book!=null){
            result.setCode(200);
            result.setSuccess(true);
            result.setMessage("查询书本成功");
            result.setData(book);
            return result;
        }else{
            result.setCode(400);
            result.setSuccess(false);
            result.setMessage("查询失败");
            return result;
        }
    }

    @PostMapping
    public Result<?> addBook(@RequestBody Book book){
        Result<Object> result = new Result<>();
        Boolean addBook = bookService.addBook(book);
        if(addBook){
            result.setCode(200);
            result.setSuccess(true);
            result.setMessage("新增成功");
            return result;
        }else{
            result.setCode(400);
            result.setSuccess(false);
            result.setMessage("新增失败");
            return result;
        }
    }

    @PutMapping
    public Result<?> updateBook(@RequestBody Book book){
        Result<Object> result = new Result<>();
        Boolean updateBook = bookService.updateBook(book);
        if(updateBook){
            result.setCode(200);
            result.setSuccess(true);
            result.setMessage("修改成功");
            return result;
        }else{
            result.setCode(400);
            result.setSuccess(false);
            result.setMessage("修改失败");
            return result;
        }
    }

    @DeleteMapping("/{id}")
    public Result<?> deleteBook(@PathVariable Integer id){
        Result<Object> result = new Result<>();
        Boolean deleteBook = bookService.deleteBook(id);
        if(deleteBook){
            result.setCode(200);
            result.setSuccess(true);
            result.setMessage("删除成功");
            return result;
        }else{
            result.setCode(400);
            result.setSuccess(false);
            result.setMessage("删除失败");
            return result;
        }
    }
}
